<?php
namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use backend\models\Manager;
use backend\models\LoginForm;
/**
 * Site controller
 */
class SiteController extends Controller
{
    public $defaultAction = 'login';
    public $layout = FALSE;
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'maxLength' => 4,
                'minLength' => 4,
            ],
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

#    public function actionTest(){
#        $model = new Manager();
#        $model->username = "admin";
#        $model->setPassword('123456');
#        $model->generateAuthKey();
#        $model->generatePasswordResetToken();
#        if($model->save()){
#            var_dump($model->getErrors());
#        }
#    }
    
    #public function actionIndex()
    #{
    #    return $this->render('index');
    #}

    public function actionLogin()
    {
        if (!Yii::$app->admin->isGuest) {
            return $this->redirect(['default/index']);
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->redirect(['login']);
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    public function actionCreateprocess(){
        $auth = Yii::$app->authManager;
        $create_post = $auth->createPermission("user/create");
        $create_post->description = "用户建立";
        $auth->add($create_post);
    }
    
    public function actionCreaterole(){
        $auth = Yii::$app->authManager;
        $create_role = $auth->createRole('超级管理员');
        $create_role->description = "超级管理员";
        $auth->add($create_role);
        
    }
    
    public function actionProcesstorole(){
        $auth = Yii::$app->authManager;
        $process = $auth->getPermission('user/create');
        $role = $auth->getRole('超级管理员');
        $auth->addChild($role,$process);
    }
    
    public function actionRoletouser(){
        $auth = Yii::$app->authManager;
        $role = $auth->getRole('超级管理员');
        $auth->assign($role,1);
    }
    
    public function actionLogout()
    {
        Yii::$app->admin->logout();

        return $this->goHome();
    }
}
